#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QUrl>

Widget::Widget(QWidget *parent)
	: QWidget(parent)
	, ui(new Ui::Widget)
{
	ui->setupUi(this);
	this->start_thread();
}

Widget::~Widget()
{
	thread->quit();
	thread->wait();
	delete ui;
}

void Widget::start_thread()
{
	ThreadRequest *req = new ThreadRequest;
	thread = new QThread;
	QObject::connect(this, &Widget::send_account, req,&ThreadRequest::wait_account_mark);
	QObject::connect(req, &ThreadRequest::send_json, this,&Widget::wait_json);
	QObject::connect(thread, SIGNAL(finished()), req, SLOT(deleteLater()));
	QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
	req->moveToThread(thread);
	qDebug() << "主线程ID : " << QThread::currentThread();
	ui->lineEdit_phone->setText("18860361934");
	ui->lineEdit_password->setText("feixin158");
	thread->start();
}
void Widget::wait_json(QByteArray json)
{
	ui->textEdit->append(json);
}
void ThreadRequest::wait_account_mark(QString phone,QString pwd)
{
	qDebug() << "子线程ID : " << QThread::currentThread();
	this->phone = phone;
	this->password = pwd;
	this->open_file();
	this->get_token_by_login();
	this->get_signid_by_token();
}
void ThreadRequest::open_file()
{
	QFile file(":/deviceToken.txt");
	file.open(QIODevice::ReadOnly);
	while(!file.atEnd())
	{
		QByteArray file_data = file.readLine();
		QString str = QTextCodec::codecForName("utf-8")->toUnicode(file_data.simplified());
		//qDebug() << str.section(":",0,0) << str.section(":",1,1);
		this->deviceToken = str.section(":",1,1);
	}
}
void ThreadRequest::get_token_by_login()
{
	manager = new QNetworkAccessManager(this);
	QNetworkRequest request;
	request.setUrl(QUrl("http://app.zhidiantianxia.cn/api/Login/pwd"));
	request.setRawHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
	request.setRawHeader("Host", "app.zhidiantianxia.cn");
	request.setRawHeader("Connection", "keep-alive");
	QByteArray data;
	data.append("appVersion=1.6.9&"
				"deviceToken=" + this->deviceToken.trimmed() + "&"
				"mobileSystem=14.6&"
				"mobileVersion=iPhone11%2C6&"
				"password=" + this->password.trimmed() + "&"
				"phone=" + this->phone.trimmed());
	qDebug()<<data;
	QNetworkReply *reply = manager->post(request,data);
	QEventLoop loop;
	QObject::connect(reply,SIGNAL(finished()),&loop,SLOT(quit()));
	connect(reply, &QNetworkReply::finished,reply,&QNetworkReply::deleteLater);
	connect(reply, &QNetworkReply::finished,[=](){
		QJsonDocument json = QJsonDocument::fromJson(reply->readAll());
		emit send_json(json.toJson());
		this->token = json.object()["data"].toString();
	});
	loop.exec();
}
void ThreadRequest::get_signid_by_token()
{
	manager = new QNetworkAccessManager(this);
	QNetworkRequest request(QUrl("http://zua.zhidiantianxia.cn/applets/signin/my?page=0&size=10"));
	request.setRawHeader("axy-phone", this->phone.toUtf8());
	request.setRawHeader("axy-token", this->token.toUtf8());
	request.setRawHeader("user-agent", "(com.axy.zhidian/1.5.9) Weex/0.18.0 1080x2340");
	request.setRawHeader("Host", "app.zhidiantianxia.cn");
	request.setRawHeader("Connection", "keep-alive");
	QNetworkReply *reply = manager->get(request);
	connect(reply, &QNetworkReply::finished,reply,&QNetworkReply::deleteLater);
	connect(reply, &QNetworkReply::finished,[=](){
		QJsonDocument json = QJsonDocument::fromJson(reply->readAll());
		qDebug() << json.toJson();
		emit send_json(json.toJson());
		this->singid = json.object().value("data").toObject().value("content").toObject().value("id").toString();
	});
}
void ThreadRequest::do_mark_by_signid()
{
	manager = new QNetworkAccessManager(this);
	QNetworkRequest request;
	request.setUrl(QUrl("http://zua.zhidiantianxia.cn/applets/signin/sign"));
	request.setRawHeader("axy-phone", this->token.toUtf8().trimmed());
	request.setRawHeader("axy-token", this->token.toUtf8().trimmed());
	request.setRawHeader("user-agent", "(com.axy.zhidian/1.5.9) Weex/0.18.0 1080x2340");
	request.setRawHeader("Host", "app.zhidiantianxia.cn");
	request.setRawHeader("Connection", "keep-alive");
	QByteArray data;
	data.append("locale=河南省郑州市二七区大学南路7-6号靠近郑州航空工业管理学院南校区&"
				"lat=34.717752&"
				"lng=113.64563&"
				"signInId=" + this->singid.trimmed() + "&");
	qDebug()<<data;
	QNetworkReply *reply = manager->post(request,data);
	connect(reply, &QNetworkReply::finished,reply,&QNetworkReply::deleteLater);
	connect(reply, &QNetworkReply::finished,[=](){
		QJsonDocument json = QJsonDocument::fromJson(reply->readAll());
		qDebug() << json;
	});
}
void ThreadRequest::logout()
{
	manager = new QNetworkAccessManager(this);
	QNetworkRequest request;
	request.setUrl(QUrl("http://zua.zhidiantianxia.cn/api/Login/logout"));
	request.setRawHeader("axy-phone", this->phone.toUtf8().trimmed());
	request.setRawHeader("axy-token", this->token.toUtf8().trimmed());
	request.setRawHeader("user-agent", "(com.axy.zhidian/1.5.9) Weex/0.18.0 1080x2340");
	request.setRawHeader("Host", "app.zhidiantianxia.cn");
	request.setRawHeader("Connection", "keep-alive");
	QByteArray data;
	data.append("phone=18860361934&token=" + this->token.trimmed());
	qDebug()<<data;
	QNetworkReply *reply = manager->post(request,data);
	connect(reply, &QNetworkReply::finished,reply,&QNetworkReply::deleteLater);
	connect(reply, &QNetworkReply::finished,[=](){
		QJsonDocument json = QJsonDocument::fromJson(reply->readAll());
		qDebug() << json;
	});
}
void Widget::on_pushButton_mark_clicked()
{
	dlg = new QDialog(this);
	dlg->setFixedSize(800,400);
	dlg->setWindowTitle("输入经纬度和位置");
	QLineEdit *longitude = new QLineEdit(dlg);
	QLineEdit *latitude = new QLineEdit(dlg);
	QLineEdit *location = new QLineEdit(dlg);
	longitude->setPlaceholderText("经度 如 : 113.646039");
	longitude->setGeometry(100,50,300,100);
	latitude->setPlaceholderText("纬度 如 : 34.716823");
	latitude->setGeometry(400,50,300,100);
	location->setPlaceholderText("地址 如 : 河南省-郑州市-二七区-郑州航空工业管理学院-大学南路7号");
	location->setGeometry(100,150,600,100);
	QPushButton *confirm = new QPushButton(dlg);
	QPushButton *cancel = new QPushButton(dlg);
	connect(confirm,&QPushButton::clicked,[=](){
		this->lng = longitude->text();
		this->lat = latitude->text();
		this->loc = location->text();
		qDebug() << lng << lat << loc;
	});
	connect(cancel,&QPushButton::clicked,[=](){
		this->lng = "";
		this->lat = "";
		this->loc = "";
		qDebug() << lng << lat << loc;
	});
	confirm->setText("确定打卡");
	confirm->setGeometry(100,300,200,50);
	confirm->show();
	cancel->setText("取消打卡");
	cancel->setGeometry(500,300,200,50);
	cancel->show();
	QDoubleValidator *pDfValidatorlnt = new QDoubleValidator(-180.0, 180.0 , 6, longitude);
	QDoubleValidator *pDfValidatorlat = new QDoubleValidator(-90.0, 90.0 , 6, latitude);
	pDfValidatorlnt->setNotation(QDoubleValidator::StandardNotation);
	pDfValidatorlat->setNotation(QDoubleValidator::StandardNotation);
	longitude->setValidator(pDfValidatorlnt);
	latitude->setValidator(pDfValidatorlat);
	longitude->show();
	latitude->show();
	location->show();
	dlg->exec();
	dlg->setAttribute(Qt::WA_DeleteOnClose);
}

void Widget::on_pushButton_login_clicked()
{
	QByteArray phone = ui->lineEdit_phone->text().toUtf8();
	QByteArray password = "axy_";
	password += ui->lineEdit_password->text().toUtf8();
	password = QCryptographicHash::hash(password,QCryptographicHash::Md5).toHex();
	emit send_account(phone, password);
}
